import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author zkq
 * @date 2022/10/14 16:29
 */
public class test {
    private static JedisPool jedisPool = getJedisPool();

    public static void main(String[] args) {
        testString();
        testList();
        testHash();
    }

    private static void testString(){
        Jedis jedis = jedisPool.getResource();
        jedis.set("k1","v1");
        String k1 = jedis.get("k1");
        System.out.println(k1);
        jedis.close();
    }

    /**
     * Jedis线程不安全 需借助线程池
     * @return 获取线程池
     */
    private static JedisPool getJedisPool(){
        JedisPoolConfig conf = new JedisPoolConfig();
        conf.setMaxTotal(10);
        conf.setMaxIdle(5);
        conf.setMinIdle(5);
        conf.setBlockWhenExhausted(true);
        conf.setMaxWaitMillis(2000);
        conf.setTestOnBorrow(true);
        String host = "hadoop102";
        int port = 6379;
        return new JedisPool(conf, host, port);
    }
    private static void testList(){
        Jedis jedis = jedisPool.getResource();
        //list是把v推进去，那么先推v1最后v3，所以查数据时v3在前面，v3的索引为0
        jedis.lpush("list","v1","v2","v3");
        System.out.println(jedis.lindex("list", 0));
        //获取列表长度 目前为6
        System.out.println(jedis.llen("list"));
    }
    private static void testHash(){
        Jedis jedis = jedisPool.getResource();
        jedis.hset("hash","field","value1");
        System.out.println(jedis.hget("hash", "field"));
    }
}
